<?php

namespace App\Http\Controllers\App;

use App\Models\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\Widthr;

class ShopController extends BaseController
{
    /**
     *店铺详情
     */
    public function  shopDetail(Request $request)
    {
        $return = [];

        $shopid   = $this->shop_id;

        $shopObj = Shop::where('status',Shop::STATUS_BEGIN);
        $data    = $shopObj->find($shopid);

        $return  = $data;

        if (empty($return)) {
            return self::json($return,'获取数据为空');
        }
        return self::json($return,'成功');
    }

    /**
     * 店铺登录密码修改
     */
    public function shopPasswordUpdate(Request $request)
    {
        $shopid   = $this->shop_id;

        //1.参数的接收
        $oldPassword    = trim($request->input('oldPassword',''));
        $newPasswordOne = trim($request->input('newPasswordOne',''));
        $newPasswordTwo = trim($request->input('newPasswordTwo',''));

        //2.参数的判断
        if (empty($oldPassword)) {
            return self::error('原始密码不能为空');
        }

        if ($newPasswordOne !== $newPasswordTwo) {
            return self::error('两次输入密码不一致');
        }

        //3.更新密码
        $obj = Shop::where('status',Shop::STATUS_BEGIN)->find($shopid);

        //4.原始密码是否输入正确
        if (User::create_pwd($oldPassword) !== $obj['password']) {
            return self::error('原始密码错误');
        }

        $obj->password = User::create_pwd($newPasswordOne);
        $res = $obj->save();

        if ($res == true) {
            return self::success('成功');
        }
        return self::error('失败');
    }

    /**
     * 店铺资金状态
     * @return [type] [description]
     */
    public function capital() 
    {
        $id      = $this->shop_id;

        $shopObj = Shop::find($id);

        //获得店铺总资金
        //$money_all = $shopObj->help_money + $shopObj->money;

        //获得审核通过
        $widthr = new Widthr();

        $data = $widthr->getWithdrInfo($id);

        $data['money_all'] = $shopObj->money;

        return self::json($data);
    }
}
